//
//  DDQuestionsRootVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/6/28.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDQuestionsRootVC.h"
#import "GlobalDefine.h"
#import "DDNetWorkAPIClient.h"
#import "DDTypeDefine.h"
#import "DDQuestionsRootTableViewCell.h"
#import "DDQuestionsLibViewController.h"
#import "PECropViewController.h"
#import "DDQuestionAnswerOptionSetViewController.h"
#import "JSONKit.h"
#import "DDQuestionBaseInfoViewController.h"
//#import "UFTextViewCustomToolbar.h"

#define Encryption_TAG 1001
#define Reward_TAG 1002
#define Help_TAG 1003
@interface DDQuestionsRootVC ()<UITextViewDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,PECropViewControllerDelegate,DDQuestionAnswerOptionSetViewControllerDelegate,UIGestureRecognizerDelegate>{
    NSMutableArray * array_picker;
    DDType_Question_QuestionPublishType paramFlagNumber_chooseType;
    DDType_Question_QuestionType paramFlag_questionType;
    DDType_Question_QuestionType flag_questionOptionSetForQuestionType;
    NSString * paramString_body;
    NSString * param_score;
    CGRect holdRect_contentOfChoose;
    BOOL flagShow_contentOfChoose;
    UINavigationController * photoProcessNav;
    UIImage * paramImage_process;
    NSString * paramImageString_process;
    NSMutableDictionary * hold_paramProcessDic;
    BOOL isCleanTableView;
    NSMutableDictionary * m_temp_oldDic;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *btn_encryption;
@property (weak, nonatomic) IBOutlet UIButton *btn_reward;
@property (weak, nonatomic) IBOutlet UIButton *btn_help;
@property (weak, nonatomic) IBOutlet UIView *contentOfPickerView;
@property (weak, nonatomic) IBOutlet UIPickerView *picker_choose;
@property (nonatomic) UIPopoverController *popover;

@end

@implementation DDQuestionsRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createUI];
    [self createData];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(publishSuccess) name:@"publishSuccess" object:nil];
}

-(void)publishSuccess{
    [self btnEncryptionClick:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    //自定义TAB BAR UI
    [self addNavigatorTitle:NSLocalizedString(@"发问", nil) parent:self.view];
    self.barViewControl.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"top_Background"]];

    [self AddRightBtnAction:@"题库出题" normal:@"" selected:@"" action:^{
        DDQuestionsLibViewController * questionLib = [[DDQuestionsLibViewController alloc]init];
        questionLib.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:questionLib animated:YES];
    }];
   
    
    [self AddLeftBtnAction:nil normal:@"top_返回" selected:@"top_返回" action:^{
        //[self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = RGB(245, 245, 245);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([DDQuestionsRootTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"questionCell"];
    
//    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissPopup)];
//    tapRecognizer.numberOfTapsRequired = 1;
//    tapRecognizer.delegate = self;
//    [self.view addGestureRecognizer:tapRecognizer];
    
}

-(void)createData{
    paramFlagNumber_chooseType = DDType_question_questionPublishType_Encryption;
    flagShow_contentOfChoose = NO;
    array_picker = [NSMutableArray array];
    [array_picker addObjectsFromArray:QuestionTypeArray];
    [self.picker_choose reloadAllComponents];
    paramFlag_questionType = DDType_question_questionType_DanXuanTi;
    hold_paramProcessDic = [NSMutableDictionary dictionary];
    self.tableView.tableFooterView = [self createFootView];

}

-(BOOL)assemblyDic{
    
    [hold_paramProcessDic removeAllObjects];
    
    for (int i = 0; i < self.tableView.visibleCells.count; i++) {
        DDQuestionsRootTableViewCell * cell = self.tableView.visibleCells[i];
        [cell.textView_body resignFirstResponder];
        if (cell.info_thisCellType == cellType_questionType_setRewardPoint) {
            [hold_paramProcessDic setObject:[NSNumber numberWithDouble:cell.tf_rewardPoint.text.doubleValue] forKey:@"score"];
        }
    }
    [hold_paramProcessDic setObject:[NSNumber numberWithInteger:paramFlag_questionType] forKey:@"type"];
    NSMutableArray * bodyArray = [NSMutableArray array];
    
    
    if (paramString_body.length > 0) {
        [bodyArray addObject:@{
                               @"type" : @"TEXT",
                               @"text" : paramString_body,
                               }];
    }else{
        if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption&& (paramFlag_questionType == DDType_question_questionType_DanXuanTi||paramFlag_questionType == DDType_question_questionType_DuoXuanTi||paramFlag_questionType == DDType_question_questionType_PanDuanTi)) {
            
            
        }else{
            [self delayHUD:@"请填写内容"];
            return NO;
        }
        
    }

    if (paramImageString_process.length > 0) {
        [bodyArray addObject:@{
                               @"type" : @"IMAGE",
                               @"url" : paramImageString_process,
                               }];
    }
    
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption) {
        if (paramFlag_questionType == DDType_question_questionType_DanXuanTi ||
            paramFlag_questionType == DDType_question_questionType_DuoXuanTi) {
            if (m_temp_oldDic == nil) {
               
                m_temp_oldDic = [NSMutableDictionary dictionaryWithDictionary:
                                 @{
                                    @"explanation" :[NSArray array],
                                    @"options" : @[
                                                    @{
                                                        @"content" : @[],
                                                        @"currect" : @"true",
                                                        @"label" : @"A",
                                                        },
                                                    @{
                                                        @"content" : @[],
                                                        @"currect" : @"false",
                                                        @"label" : @"B",
                                                        },
                                                    @{
                                                        @"content" : @[],
                                                        @"currect" : @"false",
                                                        @"label" : @"C",
                                                        },
                                                    @{
                                                        @"content" : @[],
                                                        @"currect" : @"false",
                                                        @"label" : @"D",
                                                        },
                                            
                                                    ],
                                                                                
                                   }];

            }
            [hold_paramProcessDic setValuesForKeysWithDictionary:m_temp_oldDic];
        }else if (paramFlag_questionType == DDType_question_questionType_PanDuanTi){
            
            if (flag_questionOptionSetForQuestionType != DDType_question_questionType_PanDuanTi) {
                m_temp_oldDic = nil;
            }
            
            if (m_temp_oldDic == nil) {
                m_temp_oldDic = [NSMutableDictionary dictionaryWithDictionary:
                                 @{
                                   @"explanation" :[NSArray array],
                                   @"options" : @[
                                           @{
                                               @"content" : @[
                                                       @{
                                                           @"text" : @"正确",
                                                           @"type" : @"TEXT",
                                                           },
                                                       ],
                                               @"currect" : @"false",
                                               @"label" : @"A",
                                               },
                                           @{
                                               @"content" : @[
                                                       @{
                                                           @"text" : @"错误",
                                                           @"type" : @"TEXT",
                                                           },
                                                       ],
                                               @"currect" : @"false",
                                               @"label" : @"B",
                                               },
                                            ],
                                   
                                   }];
                
            }
            [hold_paramProcessDic setValuesForKeysWithDictionary:m_temp_oldDic];

        }
        


    }else{
        if (paramFlag_questionType == DDType_question_questionType_DanXuanTi ||
            paramFlag_questionType == DDType_question_questionType_DuoXuanTi ||
            paramFlag_questionType == DDType_question_questionType_PanDuanTi) {
            
            if (flag_questionOptionSetForQuestionType != paramFlag_questionType) {
                m_temp_oldDic = nil;
            }
            
            if (m_temp_oldDic == nil) {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"请设置选项/答案" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
                return NO;
            }else{
                [hold_paramProcessDic setValuesForKeysWithDictionary:m_temp_oldDic];
            }
        }
    }
    
    [hold_paramProcessDic setObject:bodyArray forKey:@"body"];
    
    return YES;
}

-(void)saveQuestion{
    BOOL isCanSave = [self assemblyDic];
    if (isCanSave == NO) {
        return;
    }

    [self showHUD:@"正在保存..."];
    DDNetWorkAPIClient * client = [[DDNetWorkAPIClient alloc]init];
    NSDictionary * dic = @{@"data" : [hold_paramProcessDic JSONString]};
    [client mainQuestionSave:dic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        [self hideHUD];
        [self delayHUD:message];

        if (isSuccess == 1 && [message isEqualToString:@"成功"] ) {
            
        }
    }];
}

-(void)saveQuestionAndTopicSave{
    BOOL isCanSave = [self assemblyDic];
    if (isCanSave == NO) {
        return;
    }

    [self showHUD:@"正在保存..."];
    DDNetWorkAPIClient * client = [[DDNetWorkAPIClient alloc]init];

    NSDictionary * dic = @{@"data" : [hold_paramProcessDic JSONString]};
    [client mainQuestionSave:dic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if (isSuccess == 1 && [message isEqualToString:@"成功"] ) {
            NSMutableDictionary * tempDic = [NSMutableDictionary dictionary];

            [tempDic setObject:[NSNumber numberWithInteger:paramFlagNumber_chooseType] forKey:@"type"];
            [tempDic setObject:object forKey:@"questions"];
            
            [client mainTopicSave:tempDic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {



                [self hideHUD];
            }];
        }else{
            [self hideHUD];
        }
    }];
}

#pragma mark -UITableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption) {
        if (paramFlag_questionType == DDType_question_questionType_JianDaTi ||
            paramFlag_questionType == DDType_question_questionType_TianKongTi) {
            return 3;
        }else{
            return  4;
        }
    }else if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Reward){
        if (paramFlag_questionType == DDType_question_questionType_JianDaTi ||
            paramFlag_questionType == DDType_question_questionType_TianKongTi) {
            return 2;
        }else{
            return  3;
        }
    }else{
        return 1;
    }

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    t
//    return 114;
//}

-(UIView *)createFootView{
    UIView * footView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([[UIScreen mainScreen] bounds]), 114)];
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_help) {
        UIButton * btnNextStep = [UIButton buttonWithType:UIButtonTypeCustom];
        btnNextStep.frame = CGRectMake(CGRectGetMidX([[UIScreen mainScreen] bounds]) - 40, 15, 80, 40);
        [btnNextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnNextStep setTitle:@"下一步" forState:UIControlStateNormal];
        [btnNextStep setBackgroundColor:RGB(46,187,232)];
        [btnNextStep addTarget:self action:@selector(overAction:) forControlEvents:UIControlEventTouchUpInside];
        btnNextStep.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [btnNextStep setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [btnNextStep setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        btnNextStep.layer.cornerRadius = 5;
        btnNextStep.layer.masksToBounds = YES;
        [footView addSubview:btnNextStep];
        
    }else{
        //[footView setBackgroundColor:[UIColor whiteColor]];
        float btnWidth = (CGRectGetWidth([[UIScreen mainScreen] bounds]) - 50) / 3.0;
        
        UIButton *overButton = [UIButton buttonWithType:UIButtonTypeCustom];
        overButton.frame = CGRectMake( 10, 15, btnWidth, 44);
        
        [overButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [overButton setTitle:@"结束出题" forState:UIControlStateNormal];
        [overButton setBackgroundColor:RGB(46,187,232)];
        [overButton addTarget:self action:@selector(overAction:) forControlEvents:UIControlEventTouchUpInside];
        overButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [overButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [overButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:overButton];
        
        UIButton *addOneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addOneButton.frame = CGRectMake( [self xPostiong:overButton sp:15], 15, btnWidth, 44);
        
        [addOneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addOneButton setTitle:@"增加一题" forState:UIControlStateNormal];
        [addOneButton setBackgroundColor:RGB(46,187,232)];
        [addOneButton addTarget:self action:@selector(addOneAction:) forControlEvents:UIControlEventTouchUpInside];
        addOneButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [addOneButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [addOneButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:addOneButton];
        
        UIButton *saveTempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        saveTempButton.frame = CGRectMake( [self xPostiong:addOneButton sp:15], 15, btnWidth, 44);
        
        [saveTempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveTempButton setTitle:@"存至题库" forState:UIControlStateNormal];
        [saveTempButton setBackgroundColor:RGB(46,187,232)];
        [saveTempButton addTarget:self action:@selector(saveTempAction:) forControlEvents:UIControlEventTouchUpInside];
        saveTempButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [saveTempButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [saveTempButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:saveTempButton];
    }
    return footView;
}

/*
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, tableView.frame.size.width, 114)];
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_help) {
        UIButton * btnNextStep = [UIButton buttonWithType:UIButtonTypeCustom];
        btnNextStep.frame = CGRectMake(CGRectGetMidX(self.view.frame) - 40, 15, 80, 40);
        [btnNextStep setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnNextStep setTitle:@"下一步" forState:UIControlStateNormal];
        [btnNextStep setBackgroundColor:RGB(46,187,232)];
        [btnNextStep addTarget:self action:@selector(overAction:) forControlEvents:UIControlEventTouchUpInside];
        btnNextStep.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [btnNextStep setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [btnNextStep setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        btnNextStep.layer.cornerRadius = 5;
        btnNextStep.layer.masksToBounds = YES;
        [footView addSubview:btnNextStep];

    }else{
        //[footView setBackgroundColor:[UIColor whiteColor]];
        float btnWidth = (self.view.frame.size.width - 50) / 3.0;
            
        UIButton *overButton = [UIButton buttonWithType:UIButtonTypeCustom];
        overButton.frame = CGRectMake( 10, 15, btnWidth, 44);
        
        [overButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [overButton setTitle:@"结束出题" forState:UIControlStateNormal];
        [overButton setBackgroundColor:RGB(46,187,232)];
        [overButton addTarget:self action:@selector(overAction:) forControlEvents:UIControlEventTouchUpInside];
        overButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [overButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [overButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:overButton];
        
        UIButton *addOneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        addOneButton.frame = CGRectMake( [self xPostiong:overButton sp:15], 15, btnWidth, 44);
        
        [addOneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addOneButton setTitle:@"增加一题" forState:UIControlStateNormal];
        [addOneButton setBackgroundColor:RGB(46,187,232)];
        [addOneButton addTarget:self action:@selector(addOneAction:) forControlEvents:UIControlEventTouchUpInside];
        addOneButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [addOneButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [addOneButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:addOneButton];
        
        UIButton *saveTempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        saveTempButton.frame = CGRectMake( [self xPostiong:addOneButton sp:15], 15, btnWidth, 44);
        
        [saveTempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveTempButton setTitle:@"存至题库" forState:UIControlStateNormal];
        [saveTempButton setBackgroundColor:RGB(46,187,232)];
        [saveTempButton addTarget:self action:@selector(saveTempAction:) forControlEvents:UIControlEventTouchUpInside];
        saveTempButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:17.0];
        [saveTempButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [saveTempButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [footView addSubview:saveTempButton];
    }
    return footView;
}
*/

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption) {
        if (indexPath.section == 0 && indexPath.row == 2) {
            float height = 220;
            if (paramImage_process) {
                height += 70;
            }
            return height;
        }
    }else if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Reward){
        if (indexPath.section == 0 && indexPath.row == 1) {
            float height = 220;
            if (paramImage_process) {
                height += 70;
            }
            return height;
        }
    }else if (paramFlagNumber_chooseType == DDType_question_questionPublishType_help){
        if (indexPath.section == 0 && indexPath.row == 0) {
            float height = 220;
            if (paramImage_process) {
                height += 70;
            }
            return height;
        }
    }

    
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"questionCell";
    DDQuestionsRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    [cell clean];

    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption) {
        if (indexPath.row == 0) {
            cell.info_thisCellType = cellType_questionType_chooseQuestionType;
            cell.label_choosedQuestionType.text = [[QuestionTypeDic allKeysForObject:[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:paramFlag_questionType]]] firstObject];

        }else if (indexPath.row == 1){
            cell.tf_rewardPoint.text = param_score;
            cell.info_thisCellType = cellType_questionType_setRewardPoint;
        }else if (indexPath.row == 2){
            cell.info_thisCellType = cellType_questionType_body;
            cell.textView_body.text = paramString_body;
            [cell.textView_body addTextViewEndEvent:^(PlaceholderTextView *text) {
                paramString_body = text.text;
            }];
            if (paramImage_process) {
                cell.imageView_body.image = paramImage_process;
            }
        }else if (indexPath.row == 3){
            cell.info_thisCellType = cellType_questionType_setAnswer;
        }
    }else if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Reward){
        if (indexPath.row == 0) {
            cell.info_thisCellType = cellType_questionType_chooseQuestionType;
            cell.label_choosedQuestionType.text = [[QuestionTypeDic allKeysForObject:[NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:paramFlag_questionType]]] firstObject];
        }else if (indexPath.row == 1){
            cell.info_thisCellType = cellType_questionType_body;
            cell.textView_body.text = paramString_body;

            [cell.textView_body addTextViewEndEvent:^(PlaceholderTextView *text) {
                paramString_body = text.text;
            }];
            if (paramImage_process) {
                cell.imageView_body.image = paramImage_process;
            }
        }else if (indexPath.row == 2){
            cell.info_thisCellType = cellType_questionType_setAnswer;
        }
    }else if (paramFlagNumber_chooseType == DDType_question_questionPublishType_help){
        if (indexPath.row == 0) {
            cell.info_thisCellType = cellType_questionType_body;
            cell.textView_body.text = paramString_body;

            [cell.textView_body addTextViewEndEvent:^(PlaceholderTextView *text) {
                paramString_body = text.text;
            }];
            if (paramImage_process) {
                cell.imageView_body.image = paramImage_process;
            }
        }
    }

    cell.info_delegate = self;
    [cell fixCell];

    return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDQuestionsRootTableViewCell * cell = (DDQuestionsRootTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    if (cell.info_thisCellType == cellType_questionType_body || cell.info_thisCellType == cellType_questionType_setRewardPoint) {
        return nil;
    }
    return indexPath;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DDQuestionsRootTableViewCell * cell = (DDQuestionsRootTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption ||paramFlagNumber_chooseType == DDType_question_questionPublishType_Reward) {
        if (indexPath.row == 0) {
            [self showChooseContent];
        }
    }

    if (cell.info_thisCellType == cellType_questionType_setAnswer) {
        DDQuestionAnswerOptionSetViewController * vc = [[DDQuestionAnswerOptionSetViewController alloc]init];
        vc.delegate = self;
        vc.info_questionType = paramFlag_questionType;
        vc.info_oldData = m_temp_oldDic;
        vc.hidesBottomBarWhenPushed = YES;
        flag_questionOptionSetForQuestionType = paramFlag_questionType;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - picker dataSource & delegate
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return array_picker.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return array_picker[row];
}


-(float) xPostiong:(UIView *)view sp:(float)sp
{
    return view.frame.size.width+view.frame.origin.x + sp;
}

-(float) yPostiong:(UIView *)view sp:(float)sp
{
    return view.frame.size.height+view.frame.origin.y + sp;
}

#pragma makr - private
-(void)HelpAction:(UIButton *)sender
{
    sender.selected = YES;
    
    UIButton *EncryptionButton = (UIButton*)[sender.superview viewWithTag:Encryption_TAG];
    EncryptionButton.selected = NO;
    UIButton *RewardButton = (UIButton*)[sender.superview viewWithTag:Reward_TAG];
    RewardButton.selected = NO;

    
}
-(void)RewardAction:(UIButton *)sender
{
    sender.selected = YES;
    
    UIButton *EncryptionButton = (UIButton*)[sender.superview viewWithTag:Encryption_TAG];
    EncryptionButton.selected = NO;
    UIButton *HelpButton = (UIButton*)[sender.superview viewWithTag:Help_TAG];
    HelpButton.selected = NO;
}
-(void)EncryptionAction:(UIButton *)sender
{
    sender.selected = YES;
    UIButton *RewardButton = (UIButton*)[sender.superview viewWithTag:Reward_TAG];
    RewardButton.selected = NO;
    UIButton *HelpButton = (UIButton*)[sender.superview viewWithTag:Help_TAG];
    HelpButton.selected = NO;
}



//结束出题
-(void)overAction:(UIButton *)sender
{
    BOOL isCanSave = [self assemblyDic];
    if (isCanSave == NO) {
        return;
    }

    [self showHUD:@"正在保存..."];
    DDNetWorkAPIClient * client = [[DDNetWorkAPIClient alloc]init];
    NSDictionary * dic = @{@"data" : [hold_paramProcessDic JSONString]};
    [client mainQuestionSave:dic completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        [self hideHUD];

        if (isSuccess == 1 && [message isEqualToString:@"成功"] ) {
            DDQuestionBaseInfoViewController * baseInfo = [[DDQuestionBaseInfoViewController alloc]init];
            baseInfo.info_publishType = paramFlagNumber_chooseType;
            baseInfo.info_questions = @[object];
            baseInfo.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:baseInfo animated:YES];
        }else{
            [self delayHUD:message];
        }
        
        [self cleanParam];
        isCleanTableView = YES;
        [self.tableView reloadData];
    }];

   

//    [self saveQuestionAndTopicSave];
    
}

//增加一题
-(void)addOneAction:(UIButton *)sender
{
    [self saveQuestion];
    [self cleanParam];
    isCleanTableView = YES;
    [self.tableView reloadData];
}

//存至题库
-(void)saveTempAction:(UIButton *)sender
{
    [self saveQuestion];
    [self cleanParam];
    isCleanTableView = YES;
    [self.tableView reloadData];

}

-(void)cleanParam{
    paramImage_process = nil;
    paramImageString_process = @"";
    paramString_body = nil;
    m_temp_oldDic = nil;
    
}


- (IBAction)btnChooseCancel:(id)sender {
    [self hiddenChooseContent];
}
- (IBAction)btnChooseFinish:(id)sender {
    NSInteger selectIndex = [self.picker_choose selectedRowInComponent:0];
    NSString * selectString = [array_picker objectAtIndex:selectIndex];
    
    paramFlag_questionType = [[QuestionTypeDic objectForKey:selectString] integerValue];
    if (paramFlagNumber_chooseType == DDType_question_questionPublishType_Encryption) {
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
        UILabel * tempLabel = (UILabel *)[cell viewWithTag:500];
        if (tempLabel) {
            tempLabel.text = selectString;
//            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    [self hiddenChooseContent];
    [self.tableView reloadData];
}

- (IBAction)btnEncryptionClick:(id)sender {
    paramFlagNumber_chooseType = DDType_question_questionPublishType_Encryption;
   
    
    param_score = 0;
    paramFlag_questionType = DDType_question_questionType_DanXuanTi;
    paramImage_process = nil;
    paramImageString_process = @"";
    paramString_body = @"";
    m_temp_oldDic = nil;
    
    [self hiddenChooseContent];

    
    self.btn_encryption.selected = YES;
    self.btn_help.selected = NO;
    self.btn_reward.selected = NO;
    
    
    [self.tableView reloadData];
    self.tableView.tableFooterView = [self createFootView];

}

- (IBAction)btnRewardClick:(id)sender {
    paramFlagNumber_chooseType = DDType_question_questionPublishType_Reward;
    
    param_score = 0;
    paramFlag_questionType = DDType_question_questionType_JianDaTi;
    paramImage_process = nil;
    paramImageString_process = @"";
    paramString_body = @"";
    m_temp_oldDic = nil;
    
    [self hiddenChooseContent];

    
    self.btn_encryption.selected = NO;
    self.btn_help.selected = NO;
    self.btn_reward.selected = YES;
    [self.tableView reloadData];
    self.tableView.tableFooterView = [self createFootView];


}

- (IBAction)btnHelpClick:(id)sender {
    paramFlagNumber_chooseType = DDType_question_questionPublishType_help;
    
    
    param_score = 0;
    paramFlag_questionType = DDType_question_questionType_JianDaTi;
    paramImage_process = nil;
    paramImageString_process = @"";
    paramString_body = @"";
    
    m_temp_oldDic = nil;
    
    [self hiddenChooseContent];

    
    self.btn_encryption.selected = NO;
    self.btn_help.selected = YES;
    self.btn_reward.selected = NO;
    [self.tableView reloadData];
    self.tableView.tableFooterView = [self createFootView];


}


-(void)showChooseContent{
    if (holdRect_contentOfChoose.origin.y == 0) {
        holdRect_contentOfChoose = self.contentOfPickerView.frame;
    }
    if (flagShow_contentOfChoose == NO) {
        flagShow_contentOfChoose = YES;
        [UIView animateWithDuration:.3 animations:^{
            self.contentOfPickerView.frame = CGRectMake(CGRectGetMinX(self.contentOfPickerView.frame),
                                                        CGRectGetMinY(self.contentOfPickerView.frame) - CGRectGetHeight(self.contentOfPickerView.frame),
                                                        CGRectGetWidth(self.contentOfPickerView.frame),
                                                        CGRectGetHeight(self.contentOfPickerView.frame));
        } completion:^(BOOL finished) {
            
        }];
    }

}

-(void)hiddenChooseContent{
    if (flagShow_contentOfChoose == YES) {
        flagShow_contentOfChoose = NO;
        [UIView animateWithDuration:.3 animations:^{
            self.contentOfPickerView.frame = holdRect_contentOfChoose;
        } completion:^(BOOL finished) {
            
        }];
    }

}

-(void)openCamera{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.delegate = self;
        picker.allowsEditing = YES;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

-(void)openPhotoLib{

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        picker.delegate = self;
        [self presentViewController:picker animated:YES completion:nil];
    }
}

- (IBAction)openEditor:(id)sender
{
    PECropViewController *controller = [[PECropViewController alloc] init];
    controller.delegate = self;
    controller.image = paramImage_process;

    UIImage *image = paramImage_process;
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;
    CGFloat length = MIN(width, height);
    controller.imageCropRect = CGRectMake((width - length) / 2,
                                          (height - length) / 2,
                                          length,
                                          length);

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];

    [self presentViewController:navigationController animated:YES completion:nil];
}



#pragma mark - pickerImage Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    UIImage *image = info[UIImagePickerControllerOriginalImage];
    paramImage_process = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        [self openEditor:nil];
    }];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -  PECropViewControllerDelegate
- (void)cropViewController:(PECropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage{
    [controller dismissViewControllerAnimated:YES completion:nil];

    DDNetWorkAPIClient * networkClient = [[DDNetWorkAPIClient alloc]init];
    NSMutableDictionary * param = [NSMutableDictionary dictionary];

    NSData * imageData = UIImageJPEGRepresentation(croppedImage, 0.1);
    NSString * imageString = [imageData base64Encoding];

    [param setObject:imageString forKey:@"data"];
    [param setObject:@"jpg" forKey:@"extName"];
    [self showHUD:@"正在处理图片"];
    [networkClient uploadImage:param completionBlock:^(BOOL isSuccess, NSString *message, NSString *status, id object) {
        if ([[object objectForKey:@"code"] integerValue] == 0) {
            if ([object objectForKey:@"data"]) {
                paramImageString_process = [object objectForKey:@"data"];
                paramImage_process = croppedImage;
                [self.tableView reloadData];
            }
        }
        [self hideHUD];
    }];

    //http://dd5s.oss-cn-shenzhen.aliyuncs.com/upload/img/201507/201507152249266077.jpg

}

- (void)cropViewControllerDidCancel:(PECropViewController *)controller{
    [controller dismissViewControllerAnimated:YES completion:nil];
}

-(void)dismissPopup
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

#pragma mark - DDQuestionAnswerOptionSetViewControllerDelegate

-(void)DDQuestionAnswerOptionSetFinish:(NSMutableDictionary *)dic{
    m_temp_oldDic = dic;
    //[hold_paramProcessDic setValuesForKeysWithDictionary:dic];
}

@end
